<template>
  <div>
    <yt-crud
      v-bind="options"
      ref="crudRef"
      @onLoad="getData"
      :fun-props="{
        exportBtn: false,
        delBtn: false,
      }"
      :table-props="{
        selection: true,
        delBtn: true,
        viewBtn: true,
        editBtn: true,
        menuSlot: true,
        menuWidth: 300,
      }"
      :loading="state.loading"
      :total="state.total"
      v-model:page="state.page"
      v-model:query="state.query"
      @delFun="handleDelete"
      @saveFun="onSave"
      @openBeforeFun="openBeforeFun"
      @change="onChange"
    >
      <template #rightToolbar></template>
      <template #type="scope:any">
        <DictTag :options="RechargeTypeEnum" :value="scope.row.type" />
      </template>
      <template #status="scope:any">
        <el-switch v-model="scope.row.status" active-value="Enabled" inactive-value="Disabled" @change="handleStatusChange(scope.row)" />
      </template>
      <template #menuSlot="scope:any">
        <div class="menu-btn">
          <el-tooltip class="box-item" effect="dark" :content="t('business.recharge.tooltip.item')" placement="top">
            <el-button link type="primary" icon="grid" @click="handleItem(scope.row)" />
          </el-tooltip>
        </div>
      </template>
    </yt-crud>
    <ItemDialog ref="dialogRef" @success="handleSuccess" />
  </div>
</template>
<script lang="ts" setup>
import { IColumn } from '@/components/common/types/tableCommon'
import {  list as rechargeList, remove as rechargeRemove, save as rechargeSave, batchRemove as rechargeBatchRemove, changeStatus } from '@/api/business/recharge'

import ItemDialog from './modules/item.vue'

import YtCrud from '@/components/common/yt-crud.vue'
import {ComponentInternalInstance} from 'vue'
import {useI18n} from 'vue-i18n'

const { t } = useI18n()

const { proxy } = getCurrentInstance() as ComponentInternalInstance
const { EnableStatusEnum, RechargeTypeEnum } = toRefs<any>(proxy?.useEnum('enable_status_enum', 'recharge_type_enum'))

const dialogRef = ref()

const state = reactive({
  page: {
    pageSize: 30,
    pageNum: 1,
  },
  total: 0,
  loading: false,
  query: {},
})

//字典

const column = ref<IColumn[]>([
  {
    label: t('business.recharge.name'),
    searchLabelWidth: 150,
    key: 'name',
    search: true,
    formHide: false,
    colSpan: 22,
    rules: [{ required: true, message: t('business.recharge.validator.name') }],
  },
  {
    label: t('business.recharge.type'),
    key: 'type',
    type: 'select',
    componentProps: {
      options: RechargeTypeEnum,
    },
    rules: [{ required: true, message: t('business.recharge.validator.type') }],
    search: true,
    editDisabled: true,
    formHide: false,
    slot: true,
  },
  {
    label: t('business.recharge.status'),
    key: 'status',
    type: 'select',
    componentProps: {
      options: EnableStatusEnum,
    },
    rules: [{ required: true, message: t('business.recharge.validator.status') }],
    search: true,
    formHide: false,
    slot: true,
  },
  {
    label: t('crud.table.column.note'),
    key: 'note',
    hide: true,
    formHide: false,
    colSpan: 22,
    componentProps: {
      type: 'textarea',
      rows: 5,
    }
  },
  {
    label: t('crud.table.column.createTime'),
    key: 'createTime',
    tableWidth: 180,
    sortable: true,
    type: 'date',
    formHide: true,
  },
])
const crudRef = ref()
const data = ref<any[]>([])
const getData = () => {
  state.loading = true
  rechargeList({
    ...state.page,
    ...state.query,
  })
    .then((res) => {
      data.value = res.data.rows
      state.total = res.data.total
    })
    .finally(() => {
      state.loading = false
    })
}

const onSave = async ({ data, cancel }: any) => {
  state.loading = true
  await rechargeSave(toRaw(data))
  state.loading = false
  ElMessage.success(t('operate.tooltip.success'))
  cancel()
  getData()
}

const openBeforeFun = ({ type, data }) => {
  if (type === 'add') { /* empty */ } else if (type === 'update') { /* empty */ }
}

/** 状态修改  */
const handleStatusChange = async (row: any) => {
  if(!row.id)  return
  try {
    await proxy?.$modal.confirm(row.status === 'Enabled' ? t('crud.action.tooltip.enable') : t('crud.action.tooltip.disabled'))
    await changeStatus(row.id, row.status)
    ElMessage.success(t('operate.tooltip.success'))
    getData()
  } catch (err) {
    row.status = row.status === 'Enabled' ? 'Disabled' : 'Enabled'
  }
}

const handleDelete = async (row: any) => {
  state.loading = true
  if (row instanceof Array) {
    await rechargeBatchRemove(row.map((m) => m.id))
  } else {
    await rechargeRemove(row.id)
  }
  ElMessage.success(t('operate.tooltip.success'))
  state.loading = false
  getData()
}

const handleItem = (row: any) => {
  dialogRef.value.openDialog(row)
}
const handleSuccess = () => {}

const multipleSelection = ref<{ deviceId: string }[]>([])

const onChange = (data) => {
  multipleSelection.value = data
}

const options = reactive({
  ref: 'crudRef',
  data,
  column,
})
</script>

<style lang="scss" scoped></style>
